﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JMT.Data;

namespace JMT.Controls.TreeNodes
{
    public class JPackageTreeNode : JTreeNode
    {
        public JPackageTreeNode(JPackage jSolution)
            : base(jSolution)
        {
            IJContainer<JDiagram> jDiagramContainer = jSolution as IJContainer<JDiagram>;
            jDiagramContainer.Register(new JContainerUpdate<JDiagram>(JDiagramContainerUpdate));

            IJContainer<JPackage> jPackageContainer = jSolution as IJContainer<JPackage>;
            jPackageContainer.Register(new JContainerUpdate<JPackage>(JPackageContainerUpdate));

            IJContainer<JInterface> jInterfaceContainer = jSolution as IJContainer<JInterface>;
            jInterfaceContainer.Register(new JContainerUpdate<JInterface>(JInterfaceContainerUpdate));

            IJContainer<JClass> jClassContainer = jSolution as IJContainer<JClass>;
            jClassContainer.Register(new JContainerUpdate<JClass>(JClassContainerUpdate));

            List<JDiagram> diagramList = jDiagramContainer.ToList();
            foreach (var jDiagram in diagramList)
            {
                this.Items.Add(JTreeNodeFactory.Create(jDiagram));
            }

            List<JPackage> packageList = jPackageContainer.ToList();
            foreach (var jPackage in packageList)
            {
                this.Items.Add(JTreeNodeFactory.Create(jPackage));
            }

            List<JInterface> interfaceList = jInterfaceContainer.ToList();
            foreach (var jInterface in interfaceList)
            {
                this.Items.Add(JTreeNodeFactory.Create(jInterface));
            }

            List<JClass> classList = jClassContainer.ToList();
            foreach (var jClass in classList)
            {
                this.Items.Add(JTreeNodeFactory.Create(jClass));
            }
        }

        private void JDiagramContainerUpdate(object sender, JUpdateEventArgs<JDiagram> e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                int delta = e.NewStartingIndex;
                JDiagram jDiagram = e.NewItems[0] as JDiagram;
                this.Items.Insert(delta, JTreeNodeFactory.Create(jDiagram));
                this.IsExpanded = true;
            }
        }

        private void JPackageContainerUpdate(object sender, JUpdateEventArgs<JPackage> e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                int delta = (base.JModel as IJContainer<JDiagram>).ToList().Count + e.NewStartingIndex;
                JPackage jPackage = e.NewItems[0] as JPackage;
                this.Items.Insert(delta, JTreeNodeFactory.Create(jPackage));
                this.IsExpanded = true;
            }
        }

        private void JInterfaceContainerUpdate(object sender, JUpdateEventArgs<JInterface> e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                int delta = (base.JModel as IJContainer<JDiagram>).ToList().Count +
                    (base.JModel as IJContainer<JPackage>).ToList().Count + e.NewStartingIndex;
                JInterface jInterface = e.NewItems[0] as JInterface;
                this.Items.Insert(delta, JTreeNodeFactory.Create(jInterface));
                this.IsExpanded = true;
            }
        }

        private void JClassContainerUpdate(object sender, JUpdateEventArgs<JClass> e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                int delta = (base.JModel as IJContainer<JDiagram>).ToList().Count +
                    (base.JModel as IJContainer<JPackage>).ToList().Count +
                    (base.JModel as IJContainer<JInterface>).ToList().Count + e.NewStartingIndex;
                JClass jClass = e.NewItems[0] as JClass;
                this.Items.Insert(delta, JTreeNodeFactory.Create(jClass));
                this.IsExpanded = true;
            }
        }
    }
}
